<!-- 表格用操作按钮 -->
<script lang="ts" setup>
import { computed } from 'vue'
import type { PropType } from 'vue'
import { ArrowDown } from '@element-plus/icons-vue'
import type { IMyAction } from '@/types/global'
import { useAuthStore } from '@/stores/auth'

const props = defineProps({
  list: {
    type: Array as PropType<IMyAction[]>,
    default() {
      return [] as IMyAction[]
    }
  }
})

const authStore = useAuthStore()
// 最多显示多少个按钮
const maxBtn = 2

// 过滤后实际需要显示的按钮
const filterList = computed(() => {
  return props.list.filter((item) => {
    // 没传show默认为true
    const showRes = item.show ?? true
    if (showRes) {
      // 传了权限，必须show和auth同时为true才显示
      if (item.auth) {
        const authRes = authStore.btnList.find((auth) => auth.value == item.auth)
        // 权限验证通过
        if (authRes && authRes.show) {
          return true
        }
        // 权限验证不通过
        else {
          return false
        }
      }
      // 没传权限
      return true
    }
    // show为false
    return false
  })
})
// 固定显示的按钮
const showList = computed(() => {
  return filterList.value.filter((item, index) => {
    if (index < maxBtn - 1) {
      return true
    }
    // 当按钮数量等于限制数量时，才显示该按钮，否则收入更多
    else if (index == maxBtn - 1 && filterList.value.length == maxBtn) {
      return true
    }
    return false
  })
})
// 收入更多中的按钮
const moreList = computed(() => {
  return filterList.value.filter((item, index) => {
    if (index > maxBtn - 1) {
      return true
    }
    // 当按钮数量大于限制数量时，收入更多
    else if (index == maxBtn - 1 && filterList.value.length > maxBtn) {
      return true
    }
    return false
  })
})
</script>

<template>
  <div class="dui-more-btn-bg" v-if="list.length > 0">
    <!-- 固定显示的按钮 -->
    <template v-for="item in showList" :key="item.label">
      <el-button class="dui-action-btn" :type="item.type" link @click="item.onClick">{{ item.label }}</el-button>
    </template>
    <!-- 收入更多中的按钮 -->
    <el-dropdown trigger="click" v-if="moreList.length > 0">
      <div class="dui-action-more-box">
        <el-button link type="primary">更多</el-button>
        <el-icon class="dui-action-arrow">
          <ArrowDown />
        </el-icon>
      </div>
      <template #dropdown>
        <el-dropdown-menu>
          <div v-for="(item, index) in moreList" :key="index" @click="item.onClick">
            <el-dropdown-item>
              <el-button link :type="item.type">{{ item.label }}</el-button>
            </el-dropdown-item>
          </div>
        </el-dropdown-menu>
      </template>
    </el-dropdown>
  </div>
</template>

<style lang="scss" scoped>
.dui-more-btn-bg {
  display: flex;
  align-items: center;
  justify-content: center;
  .dui-action-btn {
    margin-left: 10px;
    &:first-child {
      margin-left: 0;
    }
  }

  .dui-action-more-box {
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    margin-left: 10px;
    .dui-action-btn {
      padding-right: 0;
    }
    .dui-action-arrow {
      color: $color-primary;
      font-size: 10px;
    }
  }
}
</style>
